// thinkme: would it be possible to add all of this information to individual instances of xhr?
// thinkme: what about reference back to creating FlickrAjaxManager?  would remove necessity for global ajaxManager.
function FlickrAjaxRequest(type, method, params, callback)
{
	this.type = type;
	this.method = method;
	this.params = params;
	this.callback = callback;
	this.xhr = null;
}


function FlickrAjaxManager()
{
	var that_ = this; // required for private function support

	var requestQueue_ = new Array(); // perhaps waitingRequests_?
	var inProcessRequests_ = new Array(); // perhaps processingRequests_? or similar
	this.onupdated = null; // client callback


	this.getRequestCount = function()
		{ return requestQueue_.length + inProcessRequests_.length; }


	this.get = function(method, params, callback, isPriority)
		{ requestImpl_("GET", method, params, callback, isPriority); }

	
	this.post = function(method, params, callback, isPriority)
		{ requestImpl_("POST", method, params, callback, isPriority); }


	function requestImpl_(type, method, params, callback, isPriority)
	{
		// fixme: callback should be a FlickrAjaxManager function to allow refresh or retry
		var request = new FlickrAjaxRequest(type, method, params, callback);
		if (isPriority)
			requestQueue_.unshift(request);
		else
			requestQueue_.push(request);

		processQueue_();

		// call onupdated if set
		if (that_.onupdated != null)
			that_.onupdated();
	}


	function processQueue_()
	{
		if (requestQueue_.length == 0 || inProcessRequests_.length >= 3)
			return;

		var request = requestQueue_.shift();

		if (request.type == "GET")
			request.xhr = flickrGet(request.method, request.params, getXhrCallback_());
		else if (request.type == "POST")
			request.xhr = flickrPost(request.method, request.params, getXhrCallback_());
		else {
			window.alert("invalid request type: " + request.type);
			return; // don't add this to inProcessRequests_
		}

		inProcessRequests_.push(request);
	}


	function getXhrCallback_()
	{
		var flickrAjaxManager = that_;
		return function(xhr)
			{ flickrAjaxManager.processXhrCompletion(xhr); }
	}


	this.processXhrCompletion = function(xhr)
	{
		var response = eval("(" + xhr.responseText + ")");
		if (!response.stat || response.stat != "ok") {
			window.alert("Error response:\n" + xhr.responseText);
			return false; // fixme: should retry here, if possible
		}

		// find matching request
		var requestIndex = -1;
		for (i in inProcessRequests_) {
			if (inProcessRequests_[i].xhr == xhr) {
				requestIndex = i;
				break;
			}
		}
		if (requestIndex == -1) {
			window.alert("Could not locate FlickrAjaxRequest matching finished XHR");
			return false;
		}

		// call client callback
		var request = inProcessRequests_[i];
		if (request.callback != null)
			request.callback(response); // fixme: should only send non-stat part here, if possible

		// refresh queue (remove this XHR from it)
		inProcessRequests_.splice(requestIndex, 1);

		// start processing next item if possible
		processQueue_();

		// call onupdated if set
		if (this.onupdated != null)
			this.onupdated();
	}
}
